<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "package_ease".
 *
 * @property string $id
 * @property string $code 编号
 * @property string $title 名称
 * @property int $type 资产类型
 * @property int $province 省份
 * @property string $city 城市
 * @property string $creditor 持包人名称
 * @property string $principal 本金
 * @property int $principal_range 本金额度
 * @property string $interest 利息
 * @property string $principal_interest 本息和
 * @property string $hushu 户数
 * @property string $price 当前卖方报价/预估售价（元）
 * @property string $hushu_suit 已诉户数
 * @property string $principle_suit 已诉本金
 * @property string $percent_principle_suit 已诉本金占比
 * @property string $origin 资产包信息来源
 * @property string $discount 折扣率
 * @property string $total_mortgage_principal 抵押金额（元）
 * @property string $other_note 其它补充
 * @property string $owner 上传人
 * @property string $created_at 创建时间
 */
class PackageEase extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function getDb()
    {
        return Yii::$app->get('forge');
    }

    public static function tableName()
    {
        return 'package_ease';
    }

    public function attributes()
    {
        return array_merge(parent::attributes(), ['packageEaseTransfer.remark']);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'province', 'creditor', 'principal', 'hushu',], 'required'],
            [['creditor'], 'trim'],
            [['title'], 'unique', 'targetClass' => 'backend\models\PackageEase', 'message' => '名称已存在'],
            [['province', 'principal_range'], 'integer'],
            [['type', 'principal_range', 'principal_interest', 'principle_suit', 'hushu_suit', 'interest', 'price', 'total_mortgage_principal', 'level',], 'default', 'value' => 0],
            [['code', 'other_note', 'origin',], 'default', 'value' => ''],
            [['principal', 'interest', 'price', 'principle_suit', 'total_mortgage_principal',], 'trim'],
            [['principal', 'interest', 'price', 'principle_suit', 'total_mortgage_principal', 'percent_principle_suit', 'discount',], 'number'],
            [['created_at', 'owner', 'hushu', 'principal_interest', 'other_note', 'city', 'hushu_suit', 'level', 'highlights', 'follow', 'published_at', 'updated_at', 'is_migrated'], 'safe'],
            [['code', 'title', 'creditor', 'origin',], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'code' => '编号',
            'title' => '名称',
            'type' => '资产包类型',
            'province' => '省份',
            'city' => '城市',
            'creditor' => '持包人名称',
            'principal' => '本金（元）',
            'principal_range' => '本金额度',
            'interest' => '利息（元）',
            'principal_interest' => '本息合计（元）',
            'hushu' => '户数',
            'price' => '卖价（元）',
            'hushu_suit' => '已诉户数',
            'principle_suit' => '已诉本金（元）',
            'percent_principle_suit' => '已诉本金占比',
            'origin' => '资产包信息来源',
            'discount' => '折扣率',
            'total_mortgage_principal' => '抵押金额（元）',
            'other_note' => '资产包情况介绍',
            'owner' => '上传人',
            'level' => '等级',
            'highlights' => '亮点介绍',
            'follow' => '跟进方向',
            'published_at' => '资产包发布时间',
            'updated_at' => '更新时间',
            'created_at' => '创建时间',
            'is_migrated' => '是否已迁移'
        ];
    }

    /**
     * @inheritdoc
     * @return PackageEaseQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new PackageEaseQuery(get_called_class());
    }

    public function getPackageEaseTransfer()
    {
        return $this->hasOne(PackageEaseTransfer::className(), ['package_id' => 'id']);
    }

    public function getPackageEaseAllTransfer()
    {
        return $this->hasMany(PackageEaseTransfer::className(), ['package_id' => 'id']);
    }

    public function getPublisher()
    {
        return $this->hasOne(User::className(), ['id' => 'owner']);
    }

    public static function findByTitle($title)
    {
        return self::findOne(['title' => $title]);
    }
}
